package org.andao.commons.lucene;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.andao.commons.lucene.bean.ConditionBean;
import org.andao.commons.lucene.core.extractor.DocumentListExtractor;
import org.andao.commons.lucene.core.mapper.BeanMapper;
import org.andao.commons.lucene.core.mapper.IndexMapper;
import org.andao.commons.lucene.exception.LuceneQueryException;
import org.andao.commons.lucene.index.LuceneIndexCreator;
import org.andao.commons.lucene.search.LuceneSearcher;
import org.andao.core.dao.helper.Page;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


/**
 * lucene搜索template，基于solr的实现
 * 
 * @author Seewo Software - Vick.liu (liuguanglin@cvte.cn)
 * 
 */
@Component("solrTemplate")
public class SolrTemplate implements LuceneTemplate {

	@Autowired
	@Qualifier("solrSearcher")
	private LuceneSearcher searcher;

	@Autowired
	@Qualifier("solrIndexCreator")
	private LuceneIndexCreator indexCreator;

	/**************** searcher *****************/

	/**
	 * @see LuceneTemplate#query(Page, List, IndexMapper)
	 * @throws SolrServerException
	 */
	@Override
	public Page query(Page page, List<ConditionBean> conditionBeanList, IndexMapper mapper) throws LuceneQueryException, SolrServerException {
		return searcher.query(page, conditionBeanList, mapper);
	}

	/**
	 * @see LuceneTemplate#query(Page, List, DocumentListExtractor)
	 */
	@Override
	public Page query(Page page, List<ConditionBean> conditionBeanList, DocumentListExtractor listExtractor) throws LuceneQueryException, SolrServerException {
		return searcher.query(page, conditionBeanList, listExtractor);
	}

	/************* indexCreators ***************/
	/**
	 * @see LuceneTemplate#createIndexs(List<?> sourceList, String key,
	 *      BeanMapper beanMapper)
	 */
	@Override
	public void createIndexs(List<?> sourceList, String key, BeanMapper beanMapper) throws SolrServerException, IOException {
		indexCreator.createIndexs(sourceList, key, beanMapper);
	}

	/**
	 * @see LuceneTemplate#addIndex(Object obj)
	 */
	@Override
	public void addIndex(Object obj) throws SolrServerException, IOException {
		indexCreator.addIndex(obj);
	}

	/**
	 * @throws SolrServerException 
	 * @throws IOException 
	 * @see LuceneTemplate#updateIndex(String targetKey, Object newObj)
	 */
	@Override
	public void updateIndex(String targetKey, Object newObj) throws IOException, SolrServerException {
		indexCreator.updateIndex(targetKey, newObj);
	}

	/**
	 * @throws SolrServerException 
	 * @throws IOException 
	 * @see LuceneTemplate#updateIndexs(Map<String, Object> map)
	 */
	@Override
	public void updateIndexs(Map<String, Object> map) throws IOException, SolrServerException {
		indexCreator.updateIndexs(map);
	}

	/**
	 * @see LuceneTemplate#deleteIndex(Object obj)
	 */
	@Override
	public void deleteIndex(Object obj) {
		indexCreator.deleteIndex(obj);
	}

	/**
	 * @see LuceneTemplate#destroyIndex()
	 */
	@Override
	public void destroyIndex() throws SolrServerException, IOException {
		indexCreator.destroyIndex();
	}

}
